<template>
  <div class="tag">
    <div class="wrapper-tag">
      <div class="left-tag">
        <div class="tit-left">{{ mode }}</div>
        <div class="con-left">
          <el-form :model="tagform" class="tagform">
            <el-form-item>
              <el-input
                v-model="tagform.name"
                placeholder="请输入标签名称"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-input
                v-model="tagform.value"
                placeholder="输入标签值(请输入英文，作为路由使用)"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="submitForm()"
                v-if="mode !== '管理标签'"
                >保存</el-button
              >
              <template v-if="mode === '管理标签'">
                <div class="manage">
                  <div class="left-manage">
                    <span @click="editTag()">更新</span>
                    <p @click="backAdd()">返回添加</p>
                  </div>
                  <div class="right-manage">
                    <span @click="delTag()">删除</span>
                  </div>
                </div>
              </template>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="right-tag">
        <div class="tit-right">所有标签</div>
        <div class="con-right">
          <span
            v-for="item in tagList"
            :key="item.id"
            @click="formChange(item)"
          >
            {{ item.name }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tagform: {
        name: "",
        value: "",
      },
      tagList: [],
      mode: "添加标签",
      id: "",
    };
  },
  created() {
    this.getList();
  },
  computed: {},
  methods: {
    // 获取标签列表
    getList() {
      this.$api.getTagList().then((res) => {
        this.tagList = res.data;
      });
    },
    // 添加标签
    submitForm() {
      if (this.tagform.name && this.tagform.value) {
        this.$api
          .addTag({
            ...this.tagform,
          })
          .then((res) => {
            if (res.success) {
              this.getList();
              // 清空输入的值
              this.tagform = {
                name: "",
                value: "",
              };
            } else {
              this.$message.error(res.message);
            }
          });
      } else {
        alert("不能输入为空");
      }
    },
    // 点击标签
    formChange(item) {
      this.tagform = { ...item };
      this.mode = "管理标签";
      // 保存当前点击项的id
      this.id = item.id;
    },
    // 返回添加
    backAdd() {
      this.mode = "添加标签";
      // 清空输入的值
      this.tagform = {
        name: "",
        value: "",
      };
    },
    // 删除当前标签
    delTag() {
      this.$api.delTag({ id: this.id }).then((res) => {
        if (res.success) {
          // 刷新页面
          this.getList();
          this.backAdd();
        }
      });
    },
    // 编辑标签
    editTag() {
      this.$api
        .editTag({
          ...this.tagform,
        })
        .then((res) => {
          if (res.success) {
            // 刷新页面
            this.getList();
            this.backAdd();
          }
        });
    },
  },
  components: {},
};
</script>
<style lang='scss' scoped>
.tag {
  width: 100%;
  height: 100%;
  background-color: #eff2f5;
  display: flex;
  display: flex;
  flex-direction: column;
  align-items: center;
  .wrapper-tag {
    width: 95%;
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    .left-tag {
      width: 40%;
      height: 300px;
      background-color: #fff;
      .tit-left {
        height: 60px;
        line-height: 60px;
        padding: 0 20px;
        border-bottom: 1px solid #ccc;
      }
      .con-left {
        padding: 0 20px;
        .tagform {
          margin-top: 20px;
        }
        .manage {
          display: flex;
          justify-content: space-between;
          .left-manage {
            display: flex;
            span {
              width: 80px;
              text-align: center;
              height: 45px;
              background-color: #0188fb;
              color: #fff;
              border: 1px solid #0188fb;
              outline: none;
            }
            p {
              width: 80px;
              text-align: center;
              height: 45px;
              border: 1px dashed #ccc;
            }
            p:hover {
              color: #0188fb;
              border-color: #0188fb;
            }
          }
          .right-manage {
            > span {
              display: inline-block;
              width: 80px;
              text-align: center;
              height: 45px;
              line-height: 45px;
              outline: none;
              background-color: #fff;
              border: 1px solid red;
              color: red;
            }
          }
        }
      }
    }
    .right-tag {
      width: 58%;
      height: 150px;
      background-color: #fff;
      .tit-right {
        height: 60px;
        line-height: 60px;
        padding: 0 20px;
        border-bottom: 1px solid #ccc;
      }
      .con-right {
        padding: 0 20px;
        display: flex;
        flex-wrap: wrap;
        span {
          padding: 3px 5px;
          border: 1px solid #ccc;
          margin: 10px 5px;
          color: #ccc;
          background-color: #fff;
          cursor: pointer;
        }
        span:hover {
          background-color: #4299e1;
          color: #fff;
        }
      }
    }
  }
}
</style>